﻿using System;
using System.Windows.Forms;
using SIA.Panels.Credits;
using SIA.HwAccess;

namespace SIA.HwMonitors
{
    /// <summary>Interfaccia lista segnali</summary>
    public partial class HwMonitor : Form
    {
        //Oggetto contenente tutto l'HW
        IHwDevices _hwDevices = null;

        /// <summary>Costruttore generico</summary>
        /// <param name="hwDevices"></param>
        public HwMonitor(IHwDevices  hwDevices)
        {
            InitializeComponent();

            this._hwDevices = hwDevices;
            this.toolStripStatusLabel.Text = "Collegato ad IHwDevices: " + this._hwDevices.CollectionName;

            InitializeTreeView();
            hwTreeView.ContextMenuStrip = contextMenuStrip;
        }

        void TagMonitor_msgNotifierEvent(string message, MessageType msgType)
        {
            this.Invoke(new MsgNotifier(msgNotifier), new Object[] { message, msgType });
            
        }

        public void msgNotifier(String message, MessageType msgType)
        {
            this.toolStripStatusLabel.Text = msgType.ToString() + ": " + message;
        }


        // Populates a TreeView control with example nodes. 
        private void InitializeTreeView()
        {
            
            hwTreeView.BeginUpdate();
            hwTreeView.ImageList = imageList;
            foreach (IHwDevice device in _hwDevices.Values)
            {
                hwTreeView.Nodes.Add(device.Name, device.Name, 1);
                foreach (IHwConnection conn in device.Connections.Values)
                {
                    hwTreeView.Nodes[device.Name].Nodes.Add(conn.Name, conn.Name, 1);
                    foreach (IHwSignal signal in conn.Signals.Values)
                    {
                        hwTreeView.Nodes[device.Name].Nodes[conn.Name].Nodes.Add(signal.Name, signal.Name, 2);
                    }
                }
            }
            hwTreeView.EndUpdate();
        }

        private void chiudiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (new FrmCredits()).Show();
        }

        private void tagTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null && e.Node.Parent.Parent != null)
                fillRichTextBoxSignal(e.Node.Parent.Parent.Name, e.Node.Parent.Name, e.Node.Name);            
            if (e.Node.Parent != null && e.Node.Parent.Parent == null)
                fillRichTextBoxConn(e.Node.Parent.Name, e.Node.Name);
            if (e.Node.Parent == null)
                fillRichTextBoxDevice(e.Node.Name);  

        }
      
        private void fillRichTextBoxSignal(String devName, String conName, String signalName)
        {
            try
            {
                IHwSignal hs = _hwDevices[devName].Connections[conName].Signals[signalName];

                richTextBox.Text =
                      "Nome Seganale:\t" + hs.Name + "\n"
                    + "Connessione:\t" + hs.Connection.Name + "\n"
                    + "Comment:\t" + hs.Comment + "\n"
                    + "Valore:\t\t" + hs.Value + "\n"
                    + "Info:\n" + hs.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Problema con il seganle: " + signalName,  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void fillRichTextBoxConn(String devName, String conName)
        {
            try
            {
                IHwConnection hc = _hwDevices[devName].Connections[conName];

                richTextBox.Text =
                      "Nome Connessione:" + hc.Name + "\n"
                    + "Comment:\t" + hc.Comment + "\n"

                    + "Stato:\t\t" + hc.Status.ToString() +"\n"
                    + "Info:\n" + hc.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Problema con il collegamento: " + conName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void fillRichTextBoxDevice(String devName)
        {
            try
            {
                IHwDevice hd = _hwDevices[devName];

                richTextBox.Text =
                      "Nome Device:\t" + hd.Name + "\n"
                    + "Comment:\t" + hd.Comment + "\n"
                    + "Info:\n" + hd.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Problema con il collegamento: " + devName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void connettiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (IHwDevice device in _hwDevices.Values)
                    foreach (IHwConnection conn in device.Connections.Values)
                        conn.connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Problema di connessione", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void disconnettiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
            foreach (IHwDevice device in _hwDevices.Values)
                foreach (IHwConnection conn in device.Connections.Values)
                    conn.disconnect();
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Problema di disconnessione" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void monitorModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.monitorModeToolStripMenuItem.Checked = true;
            this.debugModeToolStripMenuItem.Checked = false;
        }

        private void debugModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.monitorModeToolStripMenuItem.Checked = false;
            this.debugModeToolStripMenuItem.Checked = true;
        }

        private void tagTreeView_DoubleClick(object sender, EventArgs ev)
        {
            TreeNode e = this.hwTreeView.SelectedNode;

            if (e.LastNode == null)
            {
                Object obj = _hwDevices[e.Parent.Parent.Name].Connections[e.Parent.Name].Signals[e.Name].Value;

                if (obj is Boolean)
                    _hwDevices[e.Parent.Parent.Name].Connections[e.Parent.Name].Signals[e.Name].Value = !(Boolean)obj;
                if (obj is int)
                    _hwDevices[e.Parent.Parent.Name].Connections[e.Parent.Name].Signals[e.Name].Value = 1+((int)obj);

                //Aggiorno la visualizzazione
                fillRichTextBoxSignal(e.Parent.Parent.Name, e.Parent.Name, e.Name);   
            }            
        }

        private void infoToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            (new FrmCredits()).Show();
        }

        private void hwTreeView_Click(object sender, EventArgs e)
        {
            
        }

        private void forzaValoreToolStripMenuItem_Click(object sender, EventArgs ev)
        {
            TreeNode e = this.hwTreeView.SelectedNode;

            (new frmWriteValue(_hwDevices[e.Parent.Parent.Name].Connections[e.Parent.Name].Signals[e.Name])).Show();
        }

    }
}
